//
//  HDLanageHelper.m
//  LaoSiCheng
//
//  Created by liuyi on 2017/5/23.
//  Copyright © 2017年 liuyi. All rights reserved.
//

#import "HDLanageHelper.h"
#import "XMLReader.h"

#define documentPath   [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
#define XMLKey         @"name"
#define XMLValue       @"text"

@implementation HDLanageHelper

+ (void)transformXMLResToPlist
{
    NSArray *xmlArray = [[NSBundle mainBundle] pathsForResourcesOfType:@"xml" inDirectory:nil];
    //NSLog(@"遍历的xml数组是%@",xmlArray);//xml文件路径数组
    if (xmlArray.count > 0) {
        for (int i=0; i<xmlArray.count; i++) {
            NSString *xmlpath =[[self class] getFileNameBy:[xmlArray objectAtIndex:i]];
//           NSLog(@"xml文件的名字是--%@",xmlpath);//chinese
            NSDictionary *xmlDic = [[self class] readXmlFileWithPath:xmlpath];
//            NSLog(@"转换后的字典是\n%@",xmlDic);
            //创建plist文件
            [[self class] createLanguagePlistFileBy:xmlpath andData:xmlDic];
        }
    }
    NSLog(@"%@",documentPath);
}

#pragma mark------解析xml文件
+ (NSDictionary*)readXmlFileWithPath:(NSString*)xmlPath{
    //1、解析xml文件，保存成字典
    NSString *xmlFileString = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@",xmlPath] ofType:@"xml"];
    NSData *xmlData = [NSData dataWithContentsOfFile:xmlFileString];
    NSError *error = nil;
    
    //2、生成字典
    NSDictionary *result = [XMLReader dictionaryForXMLData:xmlData error:&error];
    //NSLog(@"result : %@", result);
    
    //3、找到有效数组
    NSArray *dataArray = [[result objectForKey:@"resources"] objectForKey:@"string"];
    //NSLog(@"处理后的数组是\n%@",dataArray);
    
    //4、数组转换为最终字典
    NSMutableDictionary *PlistDic = [[NSMutableDictionary alloc] initWithCapacity:0];
    if (dataArray.count>0) {
        for (int i=0; i<dataArray.count; i++) {
            NSDictionary *dic = [dataArray objectAtIndex:i];
            [PlistDic setObject:[dic objectForKey:XMLValue] forKey:[dic objectForKey:XMLKey]];
        }
    }
    //NSLog(@"转换后的字典是\n%@",PlistDic);
    return PlistDic;
}

#pragma mark------文件名---//chinese
+ (NSString*)getFileNameBy:(NSString*)string{
    NSArray *compmentArray = [string componentsSeparatedByString:@"/"];
    NSMutableString *pathString = [NSMutableString stringWithString:[compmentArray lastObject]];
    
    NSRange range = [pathString rangeOfString:@"."];
    NSInteger loc = range.location;
    
    [pathString deleteCharactersInRange:NSMakeRange(loc, 4)];
    
    return pathString;
}
#pragma mark------生成plist文件-----
+ (void)createLanguagePlistFileBy:(NSString*)plistPath andData:(NSDictionary*)dataDic{
    //5、在document生成plist文件
    NSString *languagePlist = [documentPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",plistPath]];
    NSFileManager* fm = [[NSFileManager alloc] init];
    if (![fm fileExistsAtPath:languagePlist]) {
        [fm createFileAtPath:languagePlist contents:nil attributes:nil];
    }
    BOOL ret = [dataDic writeToFile:languagePlist atomically:YES];
    if (ret) {
        //读文件
        NSDictionary  *reaadDic = [NSDictionary dictionaryWithContentsOfFile:languagePlist];
       // NSLog(@"dic is:%@",reaadDic);
    }
    //6、到沙盒把plist拷出来
}

#pragma mark -----




@end











